package edu.ycxy.poetry.service.serviceImpl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.ycxy.poetry.entity.ChengYu;
import edu.ycxy.poetry.mapper.ChengYuMapper;
import edu.ycxy.poetry.service.IChengYuService;
import edu.ycxy.poetry.vo.AddIdiomVo;
import edu.ycxy.poetry.vo.UpdateIdiomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author @xin
 * @createDate 2022/3/8
 * @Description
 */
@Service
public class ChengYuServiceImpl extends ServiceImpl<ChengYuMapper, ChengYu> implements IChengYuService {

    @Autowired
    private ChengYuMapper chengYuMapper;

    // 查询所有的成语,并对其进行分页处理
    @Override
    public PageInfo<ChengYu> selectListToIdiom(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ChengYu> chengYus = chengYuMapper.selectListToIdiom();
        PageInfo pageInfo = new PageInfo(chengYus);
        return pageInfo;
    }

    //根据成语名查询成语的详细信息
    @Override
    public ChengYu selectIdiomByTitle(String idiomName) {
        return chengYuMapper.selectIdiomByTitle(idiomName);
    }

    @Override
    public String addIdiom(AddIdiomVo addIdiomVo) {
        // 判断该成语是否存在
        ChengYu chengYu = chengYuMapper.selectIdiomByTitle(addIdiomVo.getTitle());
        if (chengYu == null){
            chengYuMapper.addIdiom(addIdiomVo);
            return "ok";
        }else {
            return "fail";
        }
    }

    @Override
    public PageInfo<ChengYu> findListByFuzzy(String title,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ChengYu> listByFuzzy = chengYuMapper.findListByFuzzy(title);
        PageInfo pageInfo = new PageInfo(listByFuzzy);
        return pageInfo;
    }

    @Override
    public void deleteIdiom(Integer id) {
        chengYuMapper.deleteIdiom(id);
    }

    @Override
    public ChengYu findOneById(Integer id) {
        return chengYuMapper.findOneById(id);
    }

    @Override
    public Integer updateIdiom(UpdateIdiomVo updateIdiomVo) {
        Integer integer = chengYuMapper.updateIdiom(updateIdiomVo);
        if (integer == 1){
            return 1;
        }else {
            return 2;
        }
    }
}
